package online.dgyj.rgb;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    EditText rgbIPEdit, rgbPortEdit, rgbAddressEdit;
    Switch RTUSwitch;
    Spinner RedChannelSpinner, GreenChannelSpinner, BlueChannelSpinner;
    Button BackBtn, ApplyBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_setting);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        bind();
    }

    void bind() {
        rgbIPEdit = findViewById(R.id.rgbIPEdit);
        rgbPortEdit = findViewById(R.id.rgbPortEdit);
        rgbAddressEdit = findViewById(R.id.rgbAddressEdit);

        RTUSwitch = findViewById(R.id.RTUSwitch);

        RedChannelSpinner = findViewById(R.id.RedChannelSpinner);
        GreenChannelSpinner = findViewById(R.id.GreenChannelSpinner);
        BlueChannelSpinner = findViewById(R.id.BlueChannelSpinner);

        BackBtn = findViewById(R.id.BackBtn);
        ApplyBtn = findViewById(R.id.ApplyBtn);

        BackBtn.setOnClickListener(this);
        ApplyBtn.setOnClickListener(this);

        RTUSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    rgbIPEdit.setHint("SerialName");
                    rgbPortEdit.setHint("Baud");
                    rgbIPEdit.setText("");
                    rgbPortEdit.setText("");
                }else {
                    rgbIPEdit.setHint("IP");
                    rgbPortEdit.setHint("Port");
                    rgbIPEdit.setText("");
                    rgbPortEdit.setText("");
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.BackBtn) {
            this.finish();
        } else if (v.getId() == R.id.ApplyBtn) {
            Intent intent = new Intent("LinkData");
            intent.putExtra("rgbIP", rgbIPEdit.getText().toString());
            intent.putExtra("rgbPort", Integer.parseInt(rgbPortEdit.getText().toString()));
            intent.putExtra("rgbAddress", Integer.parseInt(rgbAddressEdit.getText().toString()));
            intent.putExtra("RTULink", RTUSwitch.isChecked());
            intent.putExtra("redChannelId", Integer.parseInt(RedChannelSpinner.getSelectedItem().toString()));
            intent.putExtra("greenChannelId", Integer.parseInt(GreenChannelSpinner.getSelectedItem().toString()));
            intent.putExtra("blueChannelId", Integer.parseInt(BlueChannelSpinner.getSelectedItem().toString()));
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
            Toast.makeText(this,"应用成功",Toast.LENGTH_SHORT).show();
        }

    }
}